package com.yisingle.study.map.one;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.IntDef;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author jikun
 *         Created by jikun on 2018/3/14.
 */

public class CenterMarkerView {

    private Marker centerMarker;

    private @InfoWindowUIType
    int type = InfoWindowUIType.LOADING;

    public void addCenterMarker(AMap aMap) {
        MarkerOptions options = new MarkerOptions();
        //对应Marker.setIcon方法  设置Marker的图片
        options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_me_location));
        options.anchor(0.5F, 1);
        //拿到地图中心点的坐标。
        LatLng latLng = aMap.getCameraPosition().target;
        //把中心点的坐标转换成屏幕像素位置
        Point screenPosition = aMap.getProjection().toScreenLocation(latLng);
        //在地图上添加Marker并获取到Marker.
        centerMarker = aMap.addMarker(options);
        //给marker设置像素位置。
        centerMarker.setPositionByPixels(screenPosition.x, screenPosition.y);
        centerMarker.setAnchor(0.5F, 1);
        //这里我们就不先调用显示InfoWindow。
        //centerMarker.showInfoWindow();

    }

    public void initInfoWindowsView(final Context context, AMap aMap) {
        aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View infoWindow = LayoutInflater.from(context).inflate(
                        R.layout.map_fast_car_info_window, null);
                switch (type) {
                    case InfoWindowUIType.LOADING:
                        //Loading InfoWindow显示效果
                        infoWindow.findViewById(R.id.ll_have_net).setVisibility(View.VISIBLE);
                        infoWindow.findViewById(R.id.ll_no_net).setVisibility(View.GONE);
                        infoWindow.findViewById(R.id.ll_left).setVisibility(View.GONE);
                        infoWindow.findViewById(R.id.iv_loading).setVisibility(View.VISIBLE);

                        ImageView imageView = infoWindow.findViewById(R.id.iv_loading);
                        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
                        if (!animationDrawable.isRunning()) {
                            animationDrawable.start();
                        }

                        break;
                    case InfoWindowUIType.SUCCESS:
                        infoWindow.findViewById(R.id.ll_have_net).setVisibility(View.VISIBLE);
                        infoWindow.findViewById(R.id.ll_no_net).setVisibility(View.GONE);
                        infoWindow.findViewById(R.id.ll_left).setVisibility(View.VISIBLE);
                        infoWindow.findViewById(R.id.iv_loading).setVisibility(View.GONE);
                        break;
                    case InfoWindowUIType.FAILED:
                        infoWindow.findViewById(R.id.ll_have_net).setVisibility(View.GONE);
                        infoWindow.findViewById(R.id.ll_no_net).setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }

                return infoWindow;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });
    }


    public void hideCenterMarkerInfoWindow() {
        centerMarker.hideInfoWindow();

    }

    public void showLoadingInfoWindow() {
        type = InfoWindowUIType.LOADING;
        centerMarker.showInfoWindow();

    }

    public void showSuccessInfoWindow() {
        type = InfoWindowUIType.SUCCESS;
        centerMarker.showInfoWindow();

    }

    public void showErrorInfoWindow() {
        type = InfoWindowUIType.FAILED;
        centerMarker.showInfoWindow();

    }

    public void destorty() {
        if (null != centerMarker) {
            centerMarker.destroy();
            centerMarker.showInfoWindow();
        }
    }

    //添加支持注解的依赖到你的项目中，需要在build.gradle文件中的依赖块中添加：
    //dependencies { compile 'com.android.support:support-annotations:24.2.0' }
    @IntDef({InfoWindowUIType.LOADING, InfoWindowUIType.SUCCESS, InfoWindowUIType.FAILED})
    @Retention(RetentionPolicy.SOURCE)
    public @interface InfoWindowUIType {

        public static final int LOADING = 0;
        public static final int SUCCESS = 1;
        public static final int FAILED = 2;


    }
}
